package com.loong.seller.controller.forehead;

import com.loong.common.domain.BaseQuery;
import com.loong.common.domain.Pager;
import com.loong.common.enumerate.ShopType;
import com.loong.common.google.param.DirectionsParam;
import com.loong.common.mvc.BaseForeheadController;
import com.loong.common.param.BaseIdParam;
import com.loong.common.rest.Result;
import com.loong.common.validator.group.CommonValidate;
import com.loong.seller.common.forehead.param.ShopDeliveryPriceQueryForeheadParam;
import com.loong.seller.common.forehead.param.ShopQueryForeheadParam;
import com.loong.seller.common.forehead.result.ShopForeheadResult;
import com.loong.seller.common.forehead.result.ShopTypeResult;
import com.loong.seller.handler.ShopDeliveryRuleHandler;
import com.loong.seller.handler.ShopHandler;
import com.loong.seller.result.ShopDetail;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description:
 * @Author: hzp
 * @Date: 2020-10-28 17:08
 */
@RestController
@RequestMapping("/forehead/seller/shop")
public class ShopForeheadController extends BaseForeheadController {
    @Resource
    private ShopHandler shopHandler;

    @Resource
    private ShopDeliveryRuleHandler shopDeliveryRuleHandler;
    /**
     * 分页查询 c端使用
     * @param baseQuery 分页对象
     * @param param 分页条件参数
     * @return 返回店铺分页结果
     */
    @PostMapping("pageQuery")
    public Result pageQuery(BaseQuery baseQuery, ShopQueryForeheadParam param){
        //先查es
        return  handlerExecutor.execute(()->shopHandler.pageQuery(baseQuery, param));
    }


    /**
     * 根据店铺id查找店铺详情包括联系方式 c端使用 现在不用
     * @param param
     * @return
     */
    @PostMapping("shopDetail")
    public Result findShopByShopId(@RequestBody @Validated(value = {CommonValidate.class})BaseIdParam param){
        return handlerExecutor.execute(()->shopHandler.findDetailByShopId(param.getId()));
    }

    /**
     * 根据店铺id查找店铺 c端使用
     * @param param
     * @return
     */
    @PostMapping("shop")
    public Result findShopById(@RequestBody @Validated(value = {CommonValidate.class})BaseIdParam param){
        return handlerExecutor.execute(()->shopHandler.findShopByIdForehead(param.getId()));
    }

    /**
     * 查询自营店 只有一个
     * @return
     */
    @PostMapping("selfShop")
    public Result findSelfShop(){
        return handlerExecutor.execute(()->shopHandler.findShopByIdForehead(3L));
    }


    /**
     * 获取店铺类型 是枚举类型
     */
    @PostMapping("getShopType")
    public Result getShopType(){
        List<ShopTypeResult> list = new ArrayList<>();
        ShopType[] values = ShopType.values();
        Arrays.stream(values).filter(v->!v.getName().equals("unknown"))
                .map(v-> {
                    list.add(ShopTypeResult.builder().code(v.getCode()).name(v.getName()).desc(v.getDesc()).build());
                    return v;
                }
        ).collect(Collectors.toList());
        return handlerExecutor.execute(()->list);
    }
//    public static void main(String[] args) {
//        List<ShopTypeResult> list = new ArrayList<>();
//        ShopType[] values = ShopType.values();
//        Arrays.stream(values).filter(v->!v.getName().equals("unknown"))
//                .map(v-> {
//                    list.add(ShopTypeResult.builder().code(v.getCode()).name(v.getName()).desc(v.getDesc()).build());
//                    return v;
//                }
//        ).collect(Collectors.toList());
//        System.out.println(list);
//
//    }

    /**
     * 计算店铺配送费
     * @param param
     * @return
     */
    @PostMapping("getShopDeliveryPrice")
    public Result getShopDeliveryPrice(@RequestBody @Validated(value = {CommonValidate.class})ShopDeliveryPriceQueryForeheadParam param){
        return handlerExecutor.execute(()->shopDeliveryRuleHandler.getShopDeliveryPrice(param));
    }

    /**
     * 根据用户 和商家 经纬度 获取实际路程
     * @param directionsParam
     * @return
     */
    @PostMapping("getDistance")
    public Result getDistance(DirectionsParam directionsParam){
        return handlerExecutor.execute(()->shopDeliveryRuleHandler.getDistance(directionsParam));

    }


}
